/**
 * Created by Administrator on 2016/7/20.
 */


// var serviceHttp = 'http://intelab.ilabservice.cloud/';
var serviceHttp = 'http://inmycars.ihengtian.top:8998/';
// var serviceHttp = 'http://192.168.1.72:8998/';


var userMessage = angular.module('userMessageApp', [], function () {});


/*找回密码*/
var forGetPassword = function ($http, $scope, $location) {
    /*denglu*/
    $scope.ForGetLogin = function () {
        var unicodeStr = "";
        var companyJC = "";
        var strs = "";

        function GetRequest() {
            var url = location.search;
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }

        var Req = new Object();
        Req = GetRequest();
        companyJC = Req["companyJC"]; //E(V)
        if(companyJC=='undefined'){
            window.location.href = 'Lab_login.html';
        }else{
            window.location.href = 'Lab_login.html?company=' + companyJC;
        }
    }
    $scope.postPasswordInfo = function () {
      var unicodeStr = "";
      var companyJC = "";
      var strs = "";

      function GetRequest() {
          var url = location.search;
          var theRequest = new Object();
          if (url.indexOf("?") != -1) {
              var str = url.substr(1);
              strs = str.split("&");
              for (var i = 0; i < strs.length; i++) {
                  theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
              }
          }
          return theRequest;
      }

      var Req = new Object();
      Req = GetRequest();
      companyJC = Req["companyJC"]; //E(V)
      //登录名
      var userName = $("#userName").val();
      var number = $("#userinverdata").val();
      var companyJC = companyJC;//公司名称

      var J_user = {
          number: number,
          companyId: companyJC
      }
      if(userName && number){
          $http.post(serviceHttp + 'api/rest/operate/forget/find/password/'+ userName,JSON.stringify(J_user)).success(function (res) {
              //error==0,发送成功
              if(res.error==0){
                  if(companyJC=='undefined'){
                     alert(res.message);
                      window.location.href = 'Lab_login.html';
                  }else{
                      alert(res.message);
                      window.location.href = 'Lab_login.html?company=' + companyJC;
                  }
              }else{
                  layer.msg(res.message);
              }
          });
      }else{
          layer.msg("输入不合法！");
      }
  }
}

userMessage.controller('forGetPasswordController', forGetPassword);


/*用户详情*/
var controUserDetail = function ($http, $scope, $location) {

    var c_user = "";
    var strs = " ";

    function GetRequest() {
        var url = location.search;
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    var Req = new Object();
    Req = GetRequest();
    c_user  = Req["name"];
    $scope.redirect = function (url) {
        $location.url(url);
    }
    $scope.RedirctIndex = function () {
        window.location.href = "index.html?name=" + BASE64.encoder(c_user);
    }
    $scope.getUserListInfo = function () {
        $scope.c_user = c_user;
        // alert(Req["c_user"]);
        $http.get(serviceHttp + 'api/rest/firm/person/mine/info?name=' + c_user).success(function (res) {
            $scope.getCurrenUser = res.data;
            console.log($scope.getCurrenUser);
        });
    }


    //修改用户信息初始化
    $scope.UpdateUserInit = function () {
        $("#oldPwd").val("");
        $("#newPwd").val("");

    }

    //保存用户信息修改
    $scope.saveUpdateUserInfo = function () {
        var name = $scope.getCurrenUser.userName;
        var jobNum = $scope.getCurrenUser.jobNum;
        var password = $scope.getCurrenUser.password;//登录密码
        var oldPwd = $("#oldPwd").val(); //要输入的登录密码
        var newPwd = $("#newPwd").val(); //输入的新密码
        if (oldPwd !="" && oldPwd != null) {
            if (password == oldPwd) {
                password = newPwd;
                //   $scope.Enable = true;
            } else {
                alert("原密码输入有误！");
                password = oldPwd;
                ///  $scope.Enable = false;
                $("#oldPwd").focus();
            }
        } else {
             password = oldPwd;
            // $scope.Enable = false;
            $("#oldPwd").focus();
        }

        var mobile = $scope.getCurrenUser.mobile;
        var email = $scope.getCurrenUser.email;
        var department = $scope.getCurrenUser.department;
        var job = $scope.getCurrenUser.job;
        var telephone = $scope.getCurrenUser.telephone;
        $http.get(serviceHttp + 'api/rest/operate/update/user/'+ c_user +'?userName=' + name + '&department=' + department + '&jobNum=' + jobNum + '&job=' + job + '&password=' + password + '&mobile=' + mobile + '&telephone=' + telephone + '&email=' + email).success(function (res) {
            $scope.getUserListInfo();
        });

    }

    $scope.getUserListInfo();
}
userMessage.controller('UserDetailController', controUserDetail);


/*用户详情--验证手机号*/
userMessage.controller('userVerifyPhoneController', function ($http, $scope, $location) {

    $scope.RedirctIndex = function () {
        window.location.href = "index.html?name=" + BASE64.encoder(c_user);
    }

    //var c_user = window.location.search.substr(1).split('&')[0].substr(9);
    var c_user = "";
    var strs = " ";

    function GetRequest() {
        var url = location.search;
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    var Req = new Object();
    Req = GetRequest();
    c_user  = Req["name"];
    $scope.redirect = function (url) {
        $location.url(url);
    }
    $scope.c_user = c_user;
    $http.get(serviceHttp + '' ).success(function (res) {

    });

})